package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.AttendanceMapper;
import com.itheima.pojo.Attendance;
import com.itheima.pojo.Result;
import com.itheima.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements AttendanceService {
    @Autowired
    private AttendanceMapper attendanceMapper;

    @Override
    public Result<Page> selectPage(Page page, String keyword, Integer status) {
        // 创建LambdaQueryWrapper来构造查询条件
        LambdaQueryWrapper<Attendance> attendanceLambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 进行模糊查询，查询条件是员工姓名
        if (StringUtils.isNotBlank(keyword)) {
            attendanceLambdaQueryWrapper.like(Attendance::getEmployeeName, keyword);
        }

        // 查询条件是考勤状态
        if (status != null) {
            attendanceLambdaQueryWrapper.eq(Attendance::getStatus, status);
        }

        // 调用Mapper的selectPage方法进行分页查询
        Page page1 = attendanceMapper.selectPage(page, attendanceLambdaQueryWrapper);
        return Result.success(page1);
    }
    @Override
    public List<Attendance> getRecordsByEmployeeAndMonth(Integer employeeId, String month) {
        // 使用 LambdaQueryWrapper 构建查询条件
        LambdaQueryWrapper<Attendance> queryWrapper = new LambdaQueryWrapper<>();

        // 根据员工 ID 和月份查询考勤记录
        queryWrapper.eq(Attendance::getEmployeeId, employeeId)
                .like(Attendance::getAttendanceDate, month); // 假设 attendanceDate 是日期字符串，匹配月份

        // 查询并返回考勤记录列表
        return attendanceMapper.selectList(queryWrapper);
    }

}
